package com.resource.response;

import java.io.Serializable;
import java.util.ResourceBundle;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

/**
 * 返回结果包装类
 *
 * @author Tao.yu
 * @date 2019/11/5
 */
@Data
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class OfficeResultBody<T> implements Serializable {

	/**
	 * 状态码,200表示成功 其他表示失败
	 */
	private int code;

	/**
	 * 提示信息
	 */
	private String message;

	/**
	 * 错误信息配置
	 */
	@JsonIgnore
	private static ResourceBundle resourceBundle = ResourceBundle.getBundle("error");

	/**
	 * 提示信息国际化
	 *
	 * @param message
	 * @param defaultMessage
	 * @return
	 */
	@JsonIgnore
	private static String i18n(String message, String defaultMessage) {
		return resourceBundle.containsKey(message) ? resourceBundle.getString(message) : defaultMessage;
	}

	/**
	 * 数据
	 */
	private T data;

	private OfficeResultBody(int code, String message, T data) {
		this.code = code;
		// this.message = i18n(ErrorCode.getResultEnum(this.code).getMessage(), message);
		this.message = message;
		this.data = data;
	}




	public static <T> ResultBuilder<T> builder() {
		return new ResultBuilder<T>();
	}

	/**
	 * 构建返回对象
	 *
	 * @param <T>
	 */
	public static class ResultBuilder<T> {
		private int code;
		private String message;
		private T data;

		public ResultBuilder<T> code(int code) {
			this.code = code;
			return this;
		}

		public ResultBuilder<T> data(T data) {
			this.data = data;
			return this;
		}

		/**
		 * 请求成功 状态码 200
		 *
		 * @return
		 */
		public ResultBuilder<T> success() {
			this.code = HttpStatus.OK.value();
			this.message = "success";
			return this;
		}

		/**
		 * 请求成功 状态码 200
		 *
		 * @param data
		 * @message 带对象
		 */
		public ResultBuilder<T> success(final T data) {
			this.code = HttpStatus.OK.value();
			this.message = "success";
			this.data = data;
			return this;
		}

		/**
		 * 请求失败 状态码
		 *
		 * @param code
		 *            状态码
		 * @param msg
		 *            失败提示
		 * @return
		 */
		public ResultBuilder<T> failed(final Integer code, final String msg) {
			this.code = code;
			this.message = msg;
			return this;
		}

	}
}
